
package companyadministration;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.persistence.EntityManager;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import model.DBManager;
import model.Pelatis;
import org.jdesktop.beansbinding.Binding;
//import org.jdesktop.beansbinding.Binding;
public class ListPelatesForm extends javax.swing.JFrame {

   
    public ListPelatesForm() {
        // Αρχίζει μία συναλλαγή με την Βάση Δεδομένων
        em = DBManager.em;
        if (!em.getTransaction().isActive()) { 
            em.getTransaction().begin();
        }
        initComponents();
        // Το παράθυρο ανοίγει στο κέντρο της οθόνης
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        pelatisQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT p FROM Pelatis p");
        pelatisList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(pelatisQuery.getResultList());
        jLabel1Pelates = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1Pelates = new javax.swing.JTable();
        jButton1NeosPelatis = new javax.swing.JButton();
        jButton2EpejergasiaPelati = new javax.swing.JButton();
        jButton3DiagrafiPelati = new javax.swing.JButton();
        jButton4Akiro = new javax.swing.JButton();
        jButton5Apothikeusi = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel1Pelates.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1Pelates.setText("ΠΕΛΑΤΕΣ");

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, pelatisList, jTable1Pelates, "PelatesBind");
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${code}"));
        columnBinding.setColumnName("Code");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${eponimo}"));
        columnBinding.setColumnName("Eponimo");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${onoma}"));
        columnBinding.setColumnName("Onoma");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${tel1}"));
        columnBinding.setColumnName("Tel1");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${tel2}"));
        columnBinding.setColumnName("Tel2");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${tel3}"));
        columnBinding.setColumnName("Tel3");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${tel4}"));
        columnBinding.setColumnName("Tel4");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${adress}"));
        columnBinding.setColumnName("Adress");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(jTable1Pelates);

        jButton1NeosPelatis.setText("Νέος Πελάτης");
        jButton1NeosPelatis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1NeosPelatisActionPerformed(evt);
            }
        });

        jButton2EpejergasiaPelati.setText("Επεξεργασια Πελάτη");
        jButton2EpejergasiaPelati.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2EpejergasiaPelatiActionPerformed(evt);
            }
        });

        jButton3DiagrafiPelati.setText("Διαγραφή Πελάτη");
        jButton3DiagrafiPelati.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3DiagrafiPelatiActionPerformed(evt);
            }
        });

        jButton4Akiro.setText("Ακυρο");
        jButton4Akiro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4AkiroActionPerformed(evt);
            }
        });

        jButton5Apothikeusi.setText("Αποθήκευση");
        jButton5Apothikeusi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ApothikeusiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 729, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1Pelates)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1NeosPelatis)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton2EpejergasiaPelati)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton3DiagrafiPelati))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton4Akiro)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton5Apothikeusi)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1Pelates)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1NeosPelatis)
                    .addComponent(jButton2EpejergasiaPelati)
                    .addComponent(jButton3DiagrafiPelati))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4Akiro)
                    .addComponent(jButton5Apothikeusi))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4AkiroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4AkiroActionPerformed
        // "Εξαφάνισε" οποιαδήποτε αλλαγή εγινε στην Β.Δ.
        em.getTransaction().rollback();
        // Εκκίνησε ένα νέο transaction
        em.getTransaction().begin();
        // Κλείσε το τρέχον παράθυρο
        dispose();
    }//GEN-LAST:event_jButton4AkiroActionPerformed

    private void jButton5ApothikeusiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ApothikeusiActionPerformed
        // Οριστικοποίησε οποιαδήποτε αλλαγή στην Β.Δ.
        em.getTransaction().commit();
        // Εκκίνησε ένα νέο transaction
        em.getTransaction().begin();
        // Κλείσε το τρέχον παράθυρο
        dispose();
    }//GEN-LAST:event_jButton5ApothikeusiActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
       // "Εξαφάνισε" οποιαδήποτε αλλαγή εγινε στην Β.Δ.
        em.getTransaction().rollback();
        // Εκκίνησε ένα νέο transaction
        em.getTransaction().begin();
    }//GEN-LAST:event_formWindowClosed

    private void jButton1NeosPelatisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1NeosPelatisActionPerformed
        //Δημιουργια καινουργιου πελατη
        p=new Pelatis();
        //κανε το νεο πελατη managed
        em.persist(p);
        em.flush();//μπαινει για να παρει κωδικο ο πελατης
        
        // Άνοιξε ένα νέο παράθυρο EditPelatisForm
        EditPelatisForm epf=new EditPelatisForm(p);
        epf.setVisible(true);
        
        // Απενεργοποίησε την τρέχουσα φόρμα
        thisFrame=this;
        thisFrame.setEnabled(false);
        //************************
        epf.addWindowListener(new WindowListener(){
            public void windowClosed(WindowEvent arg0){
                System.out.println("Window close event occur");
                if(((MyWindowEvent)arg0).exitAndSave){
                    //βαλε το νεο χρηστη στη λιστα
                    
                    pelatisList.add(p);
                    
                  }else{
                    //διεγραψε το νεο χρηστη
                    em.remove(p);
                    
                }
                //Ανανεωσε τον πινακα
                //Binding b=bindingGroup.getBinding("PelatesBind");
               // b.unbind();
               // b.bind();
               // jTable1Pelates.repaint();
                
                // Ενεργοποίησε την τρέχουσα φόρμα ξανά
                thisFrame.setEnabled(true);
            }
           
            public void windowActivated(WindowEvent arg0) {
                System.out.println("Window Activated");
            }
            
            public void windowClosing(WindowEvent arg0) {
                System.out.println("Window Closing");
            }
            
            public void windowDeactivated(WindowEvent arg0) {
                System.out.println("Window Deactivated");
            }
           
            public void windowDeiconified(WindowEvent arg0) {
                System.out.println("Window Deiconified");
            }
           
            public void windowIconified(WindowEvent arg0) {
                System.out.println("Window Iconified");
            }
            
            public void windowOpened(WindowEvent arg0) {
                System.out.println("Window Opened");
            }
        });
    }//GEN-LAST:event_jButton1NeosPelatisActionPerformed

    private void jButton2EpejergasiaPelatiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2EpejergasiaPelatiActionPerformed
        //επιλεγει τη γραμμη που επιλεξαμε απο τον πινακα. Αν δεν εχει επιλεγει καμμια γραμμη επιστρεφει -1
         s=jTable1Pelates.getSelectedRow();
        //εμφανιζει τη φορμα μονο αν ειναι >0
        if (s>=0){ 
            //παρε τον αντιστοιχο πελατη   απο την λιστα pelatisList
            p=pelatisList.get(s);
            //Ανοιξε τη φορμα EditPelatisForm
        EditPelatisForm epf=new EditPelatisForm(p);
        epf.setVisible(true);
         // Απενεργοποίησε την τρέχουσα φόρμα
            thisFrame = this;
            thisFrame.setEnabled(false);
        
            //************
            epf.addWindowListener(new WindowListener() {
                public void windowClosed(WindowEvent arg0) {
	            System.out.println("Window close event occur");
                    if (((MyWindowEvent)arg0).exitAndSave){
                        // Αποθήκευση
                    } else{
                        // Φέρε το αντικείμενο στην προηγούμενη κατάσταση
                        em.refresh(p);
                    }
                    // Ανανέωσε τον πίνακα
                    Binding b = bindingGroup.getBinding("PelatesBind");
                    b.unbind();
                    b.bind();
                    jTable1Pelates.repaint();
                    // Ενεργοποίησε την τρέχουσα φόρμα ξανά
                    thisFrame.setEnabled(true);
                }
                public void windowActivated(WindowEvent arg0) {
                    System.out.println("Window Activated");
                }
                public void windowClosing(WindowEvent arg0) {
                    System.out.println("Window Closing");
                }
                public void windowDeactivated(WindowEvent arg0) {
                    System.out.println("Window Deactivated");
                }
                public void windowDeiconified(WindowEvent arg0) {
                    System.out.println("Window Deiconified");
                }
                public void windowIconified(WindowEvent arg0) {
                    System.out.println("Window Iconified");
                }
                public void windowOpened(WindowEvent arg0) {
                    System.out.println("Window Opened");
                }
            });
            //************
        }
        
    }//GEN-LAST:event_jButton2EpejergasiaPelatiActionPerformed

    private void jButton3DiagrafiPelatiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3DiagrafiPelatiActionPerformed
        // Πάρε την επιλεγμένη γραμμή
        s = jTable1Pelates.getSelectedRow();
        // Μόνο αν έχει επιλεχθεί κάποια γραμμή
        if (s >= 0) { 
            // Πάρε τον αντίστοιχο πελάτη από την λίστα customerList
            Pelatis p = pelatisList.get(s);
            // Επιβεβαίωση της διαγραφής
            int choice = JOptionPane.showConfirmDialog(this,
                                       "Διαγραφή του συγκεκριμένου πελάτη;",
                                       "Επιβεβαίωση διαγραφής πελάτη",
                                       JOptionPane.YES_NO_OPTION);
                
             if (choice != 0)
                 return;
             
             // Διέγραψε τον πελάτη
             // Από την Β.Δ.
             em.remove(p);
             // Από την λίστα
             pelatisList.remove(p);
             // Ανανέωσε το customerTable
            
        }
        
    }//GEN-LAST:event_jButton3DiagrafiPelatiActionPerformed

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1NeosPelatis;
    private javax.swing.JButton jButton2EpejergasiaPelati;
    private javax.swing.JButton jButton3DiagrafiPelati;
    private javax.swing.JButton jButton4Akiro;
    private javax.swing.JButton jButton5Apothikeusi;
    private javax.swing.JLabel jLabel1Pelates;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1Pelates;
    private java.util.List<model.Pelatis> pelatisList;
    private javax.persistence.Query pelatisQuery;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
EntityManager em;
Pelatis p;
int s;
JFrame thisFrame;
}
